﻿using System;
using System.Drawing;
using System.Windows.Forms;
using MyExcelAddIn2003.Weight;
using MyExcelAddIn2003.Properties;
using MyExcelAddIn2003.ComOPHelper;
using System.Collections;
using System.Data;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using HardwareLibrary.DVRLibrary;
using HardwareLibrary.XMLLibrary;
using HardwareLibrary.FTPLibrary;
using HardwareLibrary.MeterReaderLibrary;
using HardwareLibrary.LogLibrary;
using MyExcelAddIn2003.Core;

namespace MyExcelAddIn2003
{
    public partial class WeightFrm : Form
    {
        
        #region 当前窗体的全局变量
        public static WeightFrm GetInstance;//外部引用窗体变量
        public DVRHelper dvr0 = new DVRHelper();//监控变量
        public DVRHelper dvr1 = new DVRHelper();//监控变量
        public DVRHelper dvr2 = new DVRHelper();//监控变量
        public DVRHelper dvr3 = new DVRHelper();//监控变量
        Hashtable PicHashtable = new Hashtable();//保存图片的记录Hashtable，用于创建图片上传的XML文件
        public MeterReaderHelper mr = new MyMeterReaderHelper();//称重仪表全局变量
        //大屏幕输出全局变量
        //ID读卡器全局变量
        //IC读卡器全局变量
        FtpHelper ftp = new FtpHelper();//FTP传输全局变量
        #endregion
        #region 重写窗体绘制
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (WindowState == FormWindowState.Maximized)
            {
                tableLayoutPanel_DVR.Height = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx0.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx1.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx2.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx3.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx0.Height = tableLayoutPanel_DVR.Height;
                axDVROcx1.Height = tableLayoutPanel_DVR.Height;
                axDVROcx2.Height = tableLayoutPanel_DVR.Height;
                axDVROcx3.Height = tableLayoutPanel_DVR.Height;
            }
            if (WindowState == FormWindowState.Normal)
            {
                tableLayoutPanel_DVR.Height = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx0.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx1.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx2.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx3.Width = (tableLayoutPanel_DVR.Width - 25) / 4;
                axDVROcx0.Height = tableLayoutPanel_DVR.Height;
                axDVROcx1.Height = tableLayoutPanel_DVR.Height;
                axDVROcx2.Height = tableLayoutPanel_DVR.Height;
                axDVROcx3.Height = tableLayoutPanel_DVR.Height;
            }
        }
        #endregion
        public WeightFrm()
        {
            InitializeComponent();
            try
            {
                GetInstance = this;
                this.WindowState = FormWindowState.Maximized;
                YB_ShowText.Font = new Font("TP LCD", 30);
                #region 隐藏Excel单据界面
                Globals.ThisAddIn.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlNormal;
                Globals.ThisAddIn.Application.Height = 5;
                Globals.ThisAddIn.Application.Width = 5;
                //Globals.ThisAddIn.Application.Top = Screen.PrimaryScreen.WorkingArea.Height;
                //Globals.ThisAddIn.Application.Left = Screen.PrimaryScreen.WorkingArea.Left;
                #endregion
                StartDVRs();//启用监控
                ftp.FtpFloderListenStart(Utility.ftpInfo);//启用FTP
                VBAfunc.WeightFrm_FormLoad();//执行VBA代码
                SetStatus();
                VBAfunc.CreateBillID_Event();//创建单据编码
                listTipsBind();//车牌号码绑定
                mr.OpenComPortByParam(Utility.wtInfo);
            }
            catch(Exception ex)
            {
                LogManager.WriteLog(LogFile.Error, ex.Message);
            }
        }
        private void WeightFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                dvr0.CloseDVR();
                dvr1.CloseDVR();
                dvr2.CloseDVR();
                dvr3.CloseDVR();
                Globals.ThisAddIn.Application.Width = Screen.PrimaryScreen.WorkingArea.Width;
                Globals.ThisAddIn.Application.Height = Screen.PrimaryScreen.WorkingArea.Height;
                VBAfunc.WeightFrm_FormClosed();
            }
            catch(Exception ex)
            {
                LogManager.WriteLog(LogFile.Error, ex.Message);
            }
        }
        
        #region DVR监控方法

        private void StartDVRs()
        {
            dvr0.dvr = axDVROcx0;
            dvr1.dvr = axDVROcx1;
            dvr2.dvr = axDVROcx2;
            dvr3.dvr = axDVROcx3;
            dvr0.DVRsInit(this.Handle.ToInt32(), 0);
            dvr1.DVRsInit(this.Handle.ToInt32(), 1);
            dvr2.DVRsInit(this.Handle.ToInt32(), 2);
            dvr3.DVRsInit(this.Handle.ToInt32(), 3);
            UpdateDVRsLogoWeightNum();
        }
        public void UpdateDVRsLogoWeightNum()
        {
            UpdateDVRsLogo(textBox_jly.Text + " 重量:" + Utility.RealMeterValue.ToString("##.##"));
        }
        private void UpdateDVRsLogo(String LogoText)
        {
            dvr0.UpdateDVRsLogos(LogoText);
            dvr1.UpdateDVRsLogos(LogoText);
            dvr2.UpdateDVRsLogos(LogoText);
            dvr3.UpdateDVRsLogos(LogoText);
        }
        /// <summary>
        /// 拍照且本地化保存同时创建XML记录文件
        /// </summary>
        /// <param name="SaveFilePath"></param>
        /// <param name="strID"></param>
        /// <param name="FolderTimerStr"></param>
        private void SnapDVRsPicAndSaved(String SaveFolderPath, string strID,string FolderTimerStr)
        {
            try
            {
                if (dvr0.SnapDVRsPic(SaveFolderPath + "0_" + strID + "_" + Utility.CurrentWeightNum + ".jpg") == true)
                {
                    PicHashtable.Add("DVR0", SaveFolderPath + "0_" + strID + "_" + Utility.CurrentWeightNum + ".jpg");
                }
                if (dvr1.SnapDVRsPic(SaveFolderPath + "1_" + strID + "_" + Utility.CurrentWeightNum + ".jpg") == true)
                {
                    PicHashtable.Add("DVR1", SaveFolderPath + "1_" + strID + "_" + Utility.CurrentWeightNum + ".jpg");
                }
                if (dvr2.SnapDVRsPic(SaveFolderPath + "2_" + strID + "_" + Utility.CurrentWeightNum + ".jpg") == true)
                {
                    PicHashtable.Add("DVR2", SaveFolderPath + "2_" + strID + "_" + Utility.CurrentWeightNum + ".jpg");
                }
                if (dvr3.SnapDVRsPic(SaveFolderPath + "3_" + strID + "_" + Utility.CurrentWeightNum + ".jpg") == true)
                {
                    PicHashtable.Add("DVR3", SaveFolderPath + "3_" + strID + "_" + Utility.CurrentWeightNum + ".jpg");
                }
                XMLOp xmlop = new XMLOp();
                xmlop.CreatePicXML(SaveFolderPath + strID + "_" + Utility.CurrentWeightNum + ".xml", PicHashtable, FolderTimerStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //必须
                PicHashtable.Clear();
            }
            
        }

        #endregion
        #region 菜单按钮事件
        private void addBtn_Click(object sender, EventArgs e)
        {
            try
            {
                VBAfunc.WeightFrm_addBtn_Click();
            }
            catch
            {
            }

        }

        private void listBtn_Click(object sender, EventArgs e)
        {
            try
            {
                listFrm listfrm = new listFrm();
                AddOwnedForm(listfrm);
                listfrm.ShowDialog();
            }
            catch
            {
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            try
            {
                VBAfunc.WeightFrm_saveBtn_Click(Utility.CurrentWeightNum);
            }
            catch
            {
            }
        }

        private void seeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                picShowFrm picFrm = new picShowFrm();
                AddOwnedForm(picFrm);
                picFrm.ShowDialog();
            }
            catch
            {
            }
        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            VBAfunc.PrintBill(Utility.WeightBill, Utility.CurrentWeightNum);
        }

        private void YB_ShowText_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                YBSetForm setFrm = new YBSetForm();
                AddOwnedForm(setFrm);
                setFrm.ShowDialog();
            }
            catch
            {
            }
        }

        private void button_Set_Click(object sender, EventArgs e)
        {
            try
            {
                SetForm setFrm = new SetForm();
                AddOwnedForm(setFrm);
                setFrm.ShowDialog();
            }
            catch { }
        }

        private void btn_cm_Click(object sender, EventArgs e)
        {
            try
            {
                if (Utility.isZore == true && Utility.isStable == true)
                {
                    #region 是否重复采毛判断
                    if (Utility.YMweight > 0)
                    {
                        MessageBox.Show("禁止重复采毛!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        return;
                    }
                    #endregion
                    #region 流程走向判断
                    if (textBox_sflb.Text == "发货")
                    {
                        if (MessageBox.Show("当前操作有违正常流程走向，你确认在发货时，先进行采毛操作吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    #endregion
                    #region 关键字段判断
                    if (textBox_bdbm.Text == "")
                    {
                        MessageBox.Show("已停止计量，磅单编码不能为空。");
                        return;
                    }
                    #endregion
                    #region 文件夹判断，不然下一步会报错“文件夹不存在”
                    if (Directory.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\") == false)
                    {
                        Directory.CreateDirectory(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\");
                    }
                    #endregion
                    #region 计量次数的判定
                    if (Utility.YPweight > 0)//采毛时，如果仪皮重大于0，那么属于二次计量，否则是一次计量
                    {
                        Utility.CurrentWeightNum = 2;
                    }
                    else
                    {
                        Utility.CurrentWeightNum = 1;
                    }
                    #endregion
                    #region 拍照处理
                    SnapDVRsPicAndSaved(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\", textBox_bdbm.Text, Utility.Bill_Time);//拍照保存
                    #endregion
                    #region 再次确认窗体展现
                    WTSencondCheckFrm wtcFrm = new WTSencondCheckFrm(textBox_chmc.Text, textBox_cphm.Text, textBox_sjxm.Text, YB_ShowText.Text, YB_ShowText.Text, "采毛");
                    AddOwnedForm(wtcFrm);
                    wtcFrm.ShowDialog();
                    #endregion
                }
                else
                {
                    MessageBox.Show("数值未归零或尚未稳定，不能采重！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_cp_Click(object sender, EventArgs e)
        {
            try
            {
                if (Utility.IsEnableRegisterPi == true)
                {
                    #region 流程走向判断
                    if (textBox_sflb.Text == "收货")
                    {
                        if (MessageBox.Show("当前操作有违正常流程走向，你确认在收货时，先进行采皮操作吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
                        {
                            return;
                        }
                    }
                    #endregion
                    #region 关键字段判断
                    if (textBox_bdbm.Text == "")
                    {
                        MessageBox.Show("已停止计量，磅单编码不能为空。");
                        return;
                    }
                    #endregion
                    #region 计量次数的判定
                    if (Utility.YMweight > 0)//采毛时，如果仪皮重大于0，那么属于二次计量，否则是一次计量
                    {
                        Utility.CurrentWeightNum = 2;
                    }
                    else
                    {
                        Utility.CurrentWeightNum = 1;
                    }
                    #endregion
                    #region 使用注册皮重
                    VBAfunc.WT_CaiRegisterPi(Utility.CurrentWeightNum);
                    #endregion
                }
                else
                {
                    if (Utility.isZore == true && Utility.isStable == true)
                    {
                        #region 是否重复采皮判断
                        if (Utility.YPweight > 0)
                        {
                            MessageBox.Show("禁止重复采皮!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                        #endregion
                        #region 流程走向判断
                        if (textBox_sflb.Text == "收货")
                        {
                            if (MessageBox.Show("当前操作有违正常流程走向，你确认在收货时，先进行采皮操作吗？", "提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No)
                            {
                                return;
                            }
                        }
                        #endregion
                        #region 关键字段判断
                        if (textBox_bdbm.Text == "")
                        {
                            MessageBox.Show("已停止计量，磅单编码不能为空。");
                            return;
                        }
                        #endregion
                        #region 文件夹判断，不然下一步会报错“文件夹不存在”
                        if (Directory.Exists(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\") == false)
                        {
                            Directory.CreateDirectory(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\");
                        }
                        #endregion
                        #region 计量次数的判定
                        if (Utility.YMweight > 0)//采毛时，如果仪皮重大于0，那么属于二次计量，否则是一次计量
                        {
                            Utility.CurrentWeightNum = 2;
                        }
                        else
                        {
                            Utility.CurrentWeightNum = 1;
                        }
                        #endregion
                        #region 拍照处理
                        SnapDVRsPicAndSaved(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"Tmp\", textBox_bdbm.Text, Utility.Bill_Time);//拍照保存
                        #endregion
                        #region 再次确认窗体展现
                        WTSencondCheckFrm wtcFrm = new WTSencondCheckFrm(textBox_chmc.Text, textBox_cphm.Text, textBox_sjxm.Text, YB_ShowText.Text, YB_ShowText.Text, "采皮");
                        AddOwnedForm(wtcFrm);
                        wtcFrm.ShowDialog();
                        #endregion
                    }
                    else
                    {
                        MessageBox.Show("数值未归零或尚未稳定，不能采重！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void numericUpDown_rate_ValueChanged(object sender, EventArgs e)
        {
            Utility.RateOrNumValue = Convert.ToDouble(numericUpDown_rate.Value) / 1000;
        }

        #endregion
        //重置监控图像，用于窗体大小改变的时候
        public void reSetAllDVR()
        {
            dvr0.DVRsReset(this.Handle.ToInt32(), 0);
            dvr1.DVRsReset(this.Handle.ToInt32(), 1);
            dvr2.DVRsReset(this.Handle.ToInt32(), 2);
            dvr3.DVRsReset(this.Handle.ToInt32(), 3);
        }
        //显示真实仪表
        public void ShowRealMeterForm()
        {
            RealMeterFrm rmf = RealMeterFrm.Instance();
            AddOwnedForm(rmf);
            rmf.Show();

        }
        #region 智能提示代码
        DataTable Sourcedt;
        ListBox lb;
        private void listTipsBind()
        {
            Sourcedt = new DataTable();
            Sourcedt.Columns.Add("ID", Type.GetType("System.String"));
            Sourcedt.Columns.Add("name", Type.GetType("System.String"));
            Sourcedt.Rows.Add("j", "晋");
            Sourcedt.Rows.Add("j", "京");
            Sourcedt.Rows.Add("s", "陕");
            Sourcedt.Rows.Add("3", "豫");
            textBox_cphm.TextChanged +=new EventHandler(textBox_cphm_TextChanged);
            textBox_cphm.KeyDown +=new KeyEventHandler(textBox_cphm_KeyDown);
            textBox_cphm.LostFocus +=new EventHandler(textBox_cphm_LostFocus);
            lb = new ListBox();
            this.Controls.Add(lb);
            lb.BringToFront();
            lb.DrawItem +=new DrawItemEventHandler(lb_DrawItem);
            lb.KeyDown +=new KeyEventHandler(lb_KeyDown);
            lb.MouseUp +=new MouseEventHandler(lb_MouseUp);
            lb.MouseMove +=new MouseEventHandler(lb_MouseMove);
            lb.Width = 0;
            lb.Height = 0;
        }
        private void textBox_cphm_LostFocus(object sender, EventArgs e)
        {
            lb.Visible = false;
        }
        private void textBox_cphm_TextChanged(object sender, EventArgs e)
        {
            if (textBox_cphm.Focus() == true)
            {
                lb.Location = new Point(tableLayoutPanel_input.Location.X + textBox_cphm.Location.X, tableLayoutPanel_input.Location.Y + textBox_cphm.Location.Y + textBox_cphm.Height);
                lb.Width = textBox_cphm.Width;
                if (this.textBox_cphm.Text.Trim() != "")
                {
                    DataTable dt = getDataTable(Sourcedt, this.textBox_cphm.Text.Trim().Substring(0, 1));
                    BindList(dt);
                    this.lb.Visible = true;
                }
                else
                {
                    this.lb.Items.Clear();
                    this.lb.Visible = false;
                }
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                ws.get_Range("车牌号码", Type.Missing).Value2 = textBox_cphm.Text;
            }
        }
        private void lb_DrawItem(object sender, DrawItemEventArgs e)
        {
            lb.DrawMode = DrawMode.OwnerDrawFixed;
            e.DrawBackground();
            Brush myBrush = Brushes.Black;
            FontFamily fontFamily = new FontFamily("宋体");
            System.Drawing.Font myFont = new Font(fontFamily, 20);
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
            {
                if (e.Index > -1)
                {
                    e.Graphics.DrawString(lb.Items[e.Index].ToString(), myFont, Brushes.White, e.Bounds, StringFormat.GenericDefault);
                }
            }
            else
            {
                if (e.Index > -1)
                {
                    e.Graphics.DrawString(lb.Items[e.Index].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
                }
            }
            e.DrawFocusRectangle();
        }
        private void BindList(DataTable dt)
        {
            this.lb.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lb.Items.Add(dt.Rows[i]["name"].ToString());
            }
            if (dt.Rows.Count < 11)
            {
                this.lb.Height = 30 * dt.Rows.Count + 30;
            }
            else
            {
                this.lb.Height = 300;
            }
            this.lb.Visible = true;
        }
        private DataTable getDataTable(DataTable dt, string s)
        {
            DataRow[] drArr = dt.Select("ID LIKE '" + s + "%'");//模糊查询
            DataTable dtNew = dt.Clone();
            for (int i = 0; i < drArr.Length; i++)
            {
                dtNew.Rows.Add(drArr[i]["ID"] + textBox_cphm.Text.Substring(1), drArr[i]["name"] + textBox_cphm.Text.Substring(1).ToUpper());
                
            }
            return dtNew;
        }
        private void lb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && this.lb.Visible && this.lb.SelectedItems.Count > 0)
            {
                this.textBox_cphm.Text = this.lb.SelectedItems[0].ToString();
                this.lb.Visible = false;
                this.textBox_cphm.Focus();
            }
        }
        private void textBox_cphm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down && this.lb.Visible)
            {
                this.lb.Focus();
                if (this.lb.SelectedItems.Count > 0)
                {
                    this.lb.SetSelected(this.lb.SelectedIndex, false);
                }
                if (this.lb.Items.Count > 0)
                {
                    this.lb.SetSelected(0, true);
                }
            }
        }
        private void lb_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.lb.SelectedItems.Count > 0)
            {
                this.textBox_cphm.Text = this.lb.SelectedItems[0].ToString();
                this.lb.Visible = false;
                this.textBox_cphm.Focus();
            }
        }
        private void lb_MouseMove(object sender, MouseEventArgs e)
        {
            Point m = new Point(e.X, e.Y);
            int index = GetItemAt(this.lb, e.X, e.Y);
            if (this.lb.SelectedItems.Count > 0 && this.lb.SelectedIndex != index)
            {
                this.lb.SetSelected(this.lb.SelectedIndex, false);
            }
            if (index != -1 && this.lb.SelectedIndex != index)
            {
                this.lb.SetSelected(index, true);
            }
        }
        private int GetItemAt(ListBox listBox, int X, int Y)
        {
            int index = -1;
            for (int i = 0; i < listBox.Items.Count; i++)
            {
                System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
                if (r.Contains(new Point(X, Y)))
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
        #endregion
        #region 硬件状态
        /// <summary>
        /// 设置数值仪表的状态
        /// </summary>
        public void UpdateMeterStatusColor()
        {
            if (Utility.isStable == true)
            {
                label_Stable.BackColor = Color.Blue;
            }
            else
            {
                label_Stable.BackColor = Color.Red;
            }
            if (Utility.isZore == true)
            {
                label_Zore.BackColor = Color.Blue;
            }
            else
            {
                label_Zore.BackColor = Color.Red;
            }
        }
        /// <summary>
        /// 设置硬件运行状态
        /// </summary>
        public void SetStatus()
        {
            
            if (Utility.isRunning_YB == true)
            {
                tsStatus_YB.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_YB.Image = Resources.busy;
            }
            if (Utility.isRunning_ID == true)
            {
                tsStatus_iddkq.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_iddkq.Image = Resources.busy;
            }
            if (Utility.isRunning_PCI == true)
            {
                tsStatus_pci.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_pci.Image = Resources.busy;
            }
            if (Utility.isRunning_FTP == true)
            {
                tsStatus_ftp.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_ftp.Image = Resources.busy;
            }
            if (Utility.isRunning_OutScreen == true)
            {
                tsStatus_dpm.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_dpm.Image = Resources.busy;
            }
            if (Utility.isRunning_IC == true)
            {
                tsStatus_ic.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_ic.Image = Resources.busy;
            }
            if (Utility.isRunning_PLC)
            {
                tsStatus_plc.Image = Resources.collaboration;
            }
            else
            {
                tsStatus_plc.Image = Resources.busy;
            }
        }
        #endregion
        // 再次计量快速填充事件
        private void tSMI_FillSecWT_Click(object sender, EventArgs e)
        {

        }
        #region 文本输入框双击事件
        private void textBox_IDCardNum_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.WeightFrm_textBox_IDCardNum_DoubleClick();
        }

        private void textBox_chmc_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.WeightFrm_textBox_chmc_DoubleClick();

        }

        private void textBox_sflb_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_sflb_DoubleClick();
        }

        private void textBox_bdrq_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            VBAfunc.textBox_bdrq_MouseDoubleClick();
        }

        private void textBox_sfdh_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_sfdh_DoubleClick();
        }

        private void textBox_htdm_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_htdm_DoubleClick();
        }

        private void textBox_ggxh_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_ggxh_DoubleClick();
        }

        private void textBox_ksmc_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_ksmc_DoubleClick();
        }

        private void textBox_cydw_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_cydw_DoubleClick();
        }

        private void textBox_cd_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_cd_DoubleClick();
        }

        private void textBox_jhlb_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_jhlb_DoubleClick();
        }

        private void textBox_xhdd_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_xhdd_DoubleClick();
        }

        private void textBox_fhdd_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_fhdd_DoubleClick();
        }

        private void textBox_pczh_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_pczh_DoubleClick();
        }

        private void textBox_yffs_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_yffs_DoubleClick();
        }

        private void textBox_jsfs_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_jsfs_DoubleClick();
        }

        private void textBox_xhck_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_xhck_DoubleClick();
        }

        private void textBox_fhck_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_fhck_DoubleClick();
        }

        private void textBox_jly_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_jly_DoubleClick();
        }

        private void textBox_kpy_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_kpy_DoubleClick();
        }

        private void textBox_jby_DoubleClick(object sender, EventArgs e)
        {
            VBAfunc.textBox_jby_DoubleClick();
        }

        private void textBox_bcmz_DoubleClick(object sender, EventArgs e)
        {
            textBox_bcmz.ReadOnly = false;
        }

        private void textBox_bcpz_DoubleClick(object sender, EventArgs e)
        {
            textBox_bcpz.ReadOnly = false;
        }
        #endregion
        #region 文本框手工输入检测
        private void textBox_chmc_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_sfdh_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_htdm_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_ggxh_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_ksmc_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_cydw_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_cd_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_jhlb_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_xhdd_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_fhdd_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_pczh_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_yffs_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_jsfs_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_xhck_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_fhck_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBox_jby_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxNotInputCheck(sender, e);
        }

        private void textBoxNotInputCheck(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.Back:
                    string tbName = ((TextBox)sender).Name;
                    Control[] c = Controls.Find(tbName, true);
                    if (c[0] != null)
                    {
                        c[0].Text = "";
                    }
                    VBAfunc.ClearTextBox_Event(Utility.htVO[tbName].ToString());
                    break;
                case (char)Keys.Enter:

                    break;
                default:
                    MessageBox.Show("该文本框禁止输入,只能双击选择！");
                    e.Handled = true;
                    break;
            }
        }
        //车牌号码按键触发事件
        private void textBox_cphm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ProcessTabKey(true);
            }
        }
        //票号按键触发事件
        private void textBox_ph_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ProcessTabKey(true);
            }
            else
            {
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                ws.get_Range(Utility.htVO["textBox_ph"].ToString(), Type.Missing).Value2 = textBox_ph.Text;
            }
        }
        //司机姓名按键触发事件
        private void textBox_sjxm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ProcessTabKey(true);
            }
            else
            {
                Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
                ws.get_Range(Utility.htVO["textBox_sjxm"].ToString(), Type.Missing).Value2 = textBox_sjxm.Text;
            }
        }
        #endregion
        //车辆轴数改变事件
        private void textBox_clzs_TextChanged(object sender, EventArgs e)
        {
            Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
            ws.get_Range(Utility.htVO["textBox_clzs"].ToString(), Type.Missing).Value2 = textBox_clzs.Text;
        }
        //备注单元格改变事件
        private void textBox_bz_TextChanged(object sender, EventArgs e)
        {
            Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.Worksheets[Utility.BillWorkSheet];
            ws.get_Range(Utility.htVO["textBox_bz"].ToString(), Type.Missing).Value2 = textBox_bz.Text;
        }
        //派车证号失去焦点事件
        private void textBox_pczh_Leave(object sender, EventArgs e)
        {
            VBAfunc.textBox_pczh_Leave(textBox_pczh.Text);
        }
        //存货名称改变事件
        private void textBox_chmc_TextChanged(object sender, EventArgs e)
        {
            if (textBox_chmc.Text != "")
            {
                VBAfunc.TextBox_TextChanged(Utility.htVO["textBox_chmc"].ToString());
            }
        }
        //收发类别改变触发事件
        private void textBox_sflb_TextChanged(object sender, EventArgs e)
        {
            Utility.ShouFaLeibie = textBox_chmc.Text;
            if (textBox_chmc.Text != "")
            {
                VBAfunc.TextBox_TextChanged(Utility.htVO["textBox_sflb"].ToString());
            }
        }

        public class MyMeterReaderHelper : MeterReaderHelper
        {
            public override void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                base.Port_DataReceived(sender, e);
                #region 真实值与调整值之间的转换
                if (Utility.IsEnable_AdjustRate == true)
                {
                    if (Utility.AdjustRateType == Utility.Num)
                    {
                        if (Array.IndexOf<string>(Utility.UpArr, Utility.ShouFaLeibie) != -1)//如果收发类别属于增字段
                        {
                            Utility.OutPutValue = Utility.RealMeterValue + Utility.RateOrNumValue;
                        }
                        else if (Array.IndexOf<string>(Utility.DownArr, Utility.ShouFaLeibie) != -1)
                        {
                            Utility.OutPutValue = Utility.RealMeterValue - Utility.RateOrNumValue;
                        }
                        else
                        {
                            Utility.OutPutValue = Utility.RealMeterValue;
                        }
                    }
                    else
                    {
                        if (Array.IndexOf<string>(Utility.UpArr, Utility.ShouFaLeibie) != -1)//如果收发类别属于增字段
                        {
                            Utility.OutPutValue = Utility.RealMeterValue + (Utility.RealMeterValue * Utility.RateOrNumValue);

                        }
                        else if (Array.IndexOf<string>(Utility.DownArr, Utility.ShouFaLeibie) != -1)
                        {
                            Utility.OutPutValue = Utility.RealMeterValue - (Utility.RealMeterValue * Utility.RateOrNumValue);
                        }
                        else
                        {
                            Utility.OutPutValue = Utility.RealMeterValue;
                        }
                    }
                }
                else
                {
                    Utility.OutPutValue = Utility.RealMeterValue;
                }

                if (RealMeterFrm.Instance() != null)
                {
                    RealMeterFrm._instance.label_Real.Text = Utility.RealMeterValue.ToString("n");
                }
                //更新监控水印
                if (Utility.LockedDVRNum == false)
                {
                    WeightFrm.GetInstance.UpdateDVRsLogoWeightNum();
                }
                //更新硬件状态
                WeightFrm.GetInstance.UpdateMeterStatusColor();
                WeightFrm.GetInstance.YB_ShowText.Text = Utility.OutPutValue.ToString("n");
                #endregion
            }
        }
        #region 打印扩展事件
        private void print_gbd_Click(object sender, EventArgs e)
        {
            VBAfunc.PrintBill(Utility.WeightBill, Utility.CurrentWeightNum);
        }

        private void print_hdz_Click(object sender, EventArgs e)
        {
            VBAfunc.PrintBill(Utility.ArrivedCard, Utility.CurrentWeightNum);
        }

        private void print_zcd_Click(object sender, EventArgs e)
        {
            VBAfunc.PrintBill(Utility.OverloadBill, Utility.CurrentWeightNum);
        }
        #endregion
    }
}